// iap.js
export class Iap {
  _channelName = 'appleiap'
  IAPSdk = null
  constructor() {
    // 引入插件
    this.IAPSdk = uni.requireNativePlugin('xpc-IAPPlugin')
  }
  // 获取支付实例
  getChannels() {
    uni.getProvider({
      service: 'payment',
      success: (res) => {
        this._channel = res.providers.find((channel) => {
          return channel.id === this._channelName
        })
        if (!this._channel) {
          // 这里没有找到支付渠道，报错
        }
      }
    })
  }
  //向苹果或者谷歌服务器获取产品列表
  getProduct(productIdList) {
    return new Promise((resolve, reject) => {
      this._channel.requestProduct(
        productIdList,
        (res) => {
          resolve(res)
        },
        (err) => {
          reject(err)
        }
      )
    })
  }
  // 注册插件回掉
  initializingAndRegisteringListeners(type, callBack) {
    this.IAPSdk.initializingAndRegisteringListeners(type, callBack)
  }
  // 移除观察者
  removeObserver() {
    this.IAPSdk.removeObserver()
  }
  //获取苹果服务器已支付且未关闭的交易列表
  restoreCompletedTransactions() {
    this.IAPSdk.restorePurchase()
  }
}